package cn.foolishbird.crow.mybatis.plus.handlers;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 对象字段填充控制器代理类，实现多填充器，共同工作
 *
 * @author foolish bird
 */
public class AbstractMetaObjectHandlerProxy implements MetaObjectHandler {

	private static final Logger LOGGER = LoggerFactory.getLogger(AbstractMetaObjectHandlerProxy.class);

	/**
	 * 对象填充器集合
	 */
	private final List<AbstractMetaObjectHandler> handlers;

	public AbstractMetaObjectHandlerProxy(List<AbstractMetaObjectHandler> handlers) {
		this.handlers = handlers;
	}

	@Override
	public void insertFill(MetaObject metaObject) {
		if (CollectionUtils.isEmpty(handlers)) {
			LOGGER.warn("不存在任何对象属性填充器：{}", MetaObjectHandler.class.getName());
			return;
		}

		// 遍历执行多个对象属性填充器
		handlers.forEach(h -> h.insertFill(metaObject));
	}

	@Override
	public void updateFill(MetaObject metaObject) {
		if (CollectionUtils.isEmpty(handlers)) {
			LOGGER.warn("不存在任何对象属性填充器：{}", MetaObjectHandler.class.getName());
			return;
		}

		// 遍历执行多个对象属性填充器
		handlers.forEach(h ->h.updateFill(metaObject));
	}
}
